/*
 * MainWindow.cpp
 *
 *  Created on: Feb 2, 2010
 *      Author: dcrown
 */
#include <windows.h>

#include "Logger.h"
#include "BasicWindow.h"
#include "..\Graphics\DXManager.h"
#include "InputManager.h"

#define CLASS_NAME "BasicWindow"

extern DXManager* gpDX;
extern InputManager* gpIM;
extern BasicWindow* gpBW;

HWND BasicWindow::Create(HINSTANCE hInstance, int nCmdShow)
{
	this->nCmdShow = nCmdShow;
	hwnd = RegisterWindow(hInstance);
	SetDimensions();
	Logger::getInstance().LOG(Logger::DEBUG, "BasicWindow Created");
	return hwnd;
}

void BasicWindow::Show()
{
	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);
}


//WndProc - based on theForger's tutorial - http://winprog.org/tutorial/simple_window.html
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    //these should be done with WM_CHAR
		case WM_KEYDOWN:
		case WM_KEYUP:
		//case WM_CHAR:
			gpIM->ProcessKeyIn(msg, wParam, lParam);
			break;
		case WM_LBUTTONDOWN:
		case WM_LBUTTONUP:
			gpIM->ProcessMouseIn(msg, wParam, lParam);
			break;
		case WM_RBUTTONDOWN:
			//gpIM->ProcessMouseIn(msg, wParam, lParam);
			break;
		case WM_MBUTTONDOWN:
			//gpIM->ProcessMouseIn(msg, wParam, lParam);
			break;
		case WM_SIZE:
			Logger::getInstance().LOG(Logger::DEBUG, "Resized");
			break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

HWND BasicWindow::RegisterWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wc;//the window class
	HWND hwnd;

	//Step 1: Registering the Window Class
	wc.cbSize        = sizeof(WNDCLASSEX);//size
	wc.style         = 0;
	wc.lpfnWndProc   = WndProc;//function to process events
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;//program this window belongs to
	wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);//large icon
	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);//cursor
	wc.hbrBackground = 0;//bg color
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = CLASS_NAME;//window class name
	wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);//small icon

	//register the window class
	if(!RegisterClassEx(&wc))
	{
		Logger::getInstance().LOG(Logger::FATAL, "Window Registration Failed!");
		return NULL;
	}

	// Step 2: Creating the Window
	hwnd = CreateWindowEx(
		0,
		CLASS_NAME,
		"DXWindow",
		WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
		CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT,
		NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		Logger::getInstance().LOG(Logger::FATAL, "Window Creation Failed!");
		return NULL;
	}

	return hwnd;
}

void BasicWindow::SetDimensions()
{
	RECT rect;
	if(GetClientRect(hwnd, &rect))
	{
		width = rect.right - rect.left;
		height = rect.bottom - rect.top;
	}
}
